package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.ProductQueryObject;
import cn.wolfcode.wms.service.BrandService;
import cn.wolfcode.wms.service.ProductService;
import cn.wolfcode.wms.service.RoleService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.util.UploadUtil;
import cn.wolfcode.wms.vo.JSONResult;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ServletContext ctx;

    @RequiredPermission("商品查询")
    @RequestMapping("/list")
    public String list(Map map, ProductQueryObject qo) {
        List<Brand> brands = brandService.selectAll();
        map.put("brands", brands);
        map.put("qo", qo);
        PageResult result = productService.query(qo);
        map.put("result", result);
        return "product/list";
    }
    @RequestMapping("/selectProductList")
    public String selectProductList(Map map, ProductQueryObject qo) {
        List<Brand> brands = brandService.selectAll();
        map.put("brands", brands);
        map.put("qo", qo);
        PageResult result = productService.query(qo);
        map.put("result", result);
        return "product/selectProductList";
    }

    @RequiredPermission("商品编辑")
    @RequestMapping("/input")
    public String input(Map map, Long id) {
        List<Brand> brands = brandService.selectAll();
        map.put("brands", brands);
        if (id != null && id != 0) {
            Product product = productService.getById(id);
            map.put("product", product);
        }
        return "/product/input";
    }

    @RequiredPermission("商品删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id,String imagePath) {
        try {
            if (!StringUtils.isEmpty(imagePath)) {
                UploadUtil.deleteFile(ctx,imagePath);
            }
            productService.deleteById(id);

        } catch (Exception e) {
            return JSONResult.mark(e.getLocalizedMessage());
        }
        return JSONResult.success("删除成功");
    }

    @RequiredPermission("商品新增/修改")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Product product, MultipartFile img) {
        try {
            //更新是有上传新图片,且原来图片不为空,将原来图片删除
            if (img!=null && !StringUtils.isEmpty(product.getImagePath())){
                UploadUtil.deleteFile(ctx,product.getImagePath());
            }
            //上传图片
            if(img!=null){
                String imagePath= UploadUtil.upload(img,ctx.getRealPath("/upload"));
                product.setImagePath(imagePath);
            }
        productService.updateOrSave(product,img);
        }catch (Exception e){
            return JSONResult.mark(e.getLocalizedMessage());
        }
        return JSONResult.success("修改成功");
    }
    @RequiredPermission("批量删除")
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteall(Long[] ids) {
        try {
            productService.deleteAll(ids);
            System.out.println("nmsl");
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getLocalizedMessage());
        }
        return JSONResult.success("删除成功");
    }

}
